﻿using Infrastructure;
using Lan.Dto;
using Lan.Model;
using Lan.Repository;
using Lan.ServiceCore.IService;
using Model;
using SqlSugar;
using System.Linq.Expressions;

namespace Lan.ServiceCore.Services
{
    [AppService(ServiceType = typeof(IAlarmService), ServiceLifetime = LifeTime.Transient)]
    public class AlarmService : Repository<AlarmModel>, IAlarmService
    {
        public AlarmService() { }


        public PagedInfo<AlarmDto> GetList(AlarmQueryDto parm)
        {
            var exp = Expressionable.Create<AlarmModel>();
            exp.AndIF(!string.IsNullOrEmpty(parm.ip), u => u.CameraIp.Contains(parm.ip));
            exp.AndIF(!string.IsNullOrEmpty(parm.AreaId.ToString()), u => u.AreaId == parm.AreaId);
            //if (parm.defenceareaId != 0)
            //{
            //    exp.AndIF(!string.IsNullOrEmpty(parm.defenceareaId.ToString()), u => u.BindingAreaId == parm.defenceareaId);
            //}

            var response = Queryable()
            .LeftJoin<CameraModel>((u, c) => u.CameraIp == c.Ip)
            .Where(exp.ToExpression())
            .Select((u, c) => new AlarmModel
            {

                Id = u.Id,
                CameraIp = u.CameraIp,
                AreaId = u.AreaId,
                AreaName = u.AreaName,
                DateTime = u.DateTime,
                DealWith = u.DealWith,
                VideoName = u.VideoName,
                Level = u.Level,

                //Username = c.Username,
                //Password = c.Password,
            })
            .OrderBy(u => u.Id, OrderByType.Desc)
            .ToPage<AlarmModel, AlarmDto>(parm);

            return response;
        }
        public List<AlarmModel> GetListRef(AlarmQueryDto parm)
        {
            var exp = Expressionable.Create<AlarmModel>();
            var response = Queryable().Take(10).OrderBy(u => u.Id, OrderByType.Desc).ToList();
            return response;
        }

        public AlarmModel AddAlarm(AlarmModel alarm)
        {
            return Insertable(alarm).ExecuteReturnEntity();
        }

        public int GatMaxAlarmId(int AreaId)
        {
            //db.Queryable<Order>().Max(it => it.Id);//同步 
            //db.Queryable<Order>().MaxAsync(it => it.Id);//异步
            Expression<Func<AlarmModel, bool>> expression = x => x.AreaId == AreaId;
            var maxID = 0;//Context.Queryable<AlarmModel>().Where(expression).Max(x => x.Id);
            return maxID;
        }

        public AlarmModel GetInfo(int Id)
        {
            var response = Queryable()
                .Where(x => x.Id == Id)
                .First();
            return response;
        }

        public int GetInfo(int AreaId, string DealWith)
        {
            var response = Queryable()
                .Where(it => it.AreaId == AreaId && it.DealWith == DealWith).Max(it => it.Id);

            return response;
        }
        public int GetInfos(int AreaId)
        {
            var response = Queryable()
                .Where(x => x.AreaId == AreaId).Max(p => p.Id);

            return response;
        }
        public int UpdateAlarm(int[] ids)
        {
            var result = -1;
            foreach (var id in ids)
            {
                result = Update(w => w.Id == id, it => new AlarmModel()
                {
                    DealWith = "已处理"
                });
            }
            return result;
        }
        public int DeleteAlarm(int id)
        {
            return DeleteTable(id); ;
        }
    }
}
